package com.tuzhi.test.sys.service.impl;

import com.tuzhi.base.jpa.repository.BaseRepo;
import com.tuzhi.base.service.impl.AbsBaseService;
import com.tuzhi.test.sys.jpa.entity.SysRoleEntity;
import com.tuzhi.test.sys.jpa.entity.SysUserEntity;
import com.tuzhi.test.sys.jpa.repository.SysUserRepo;
import com.tuzhi.test.sys.service.SysRoleService;
import com.tuzhi.test.sys.service.SysUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SysUserServiceImpl extends AbsBaseService<SysUserEntity, Long> implements SysUserService {

    private static final Logger log = LoggerFactory.getLogger(SysUserServiceImpl.class);

    @Autowired
    SysUserRepo repo;

    @Autowired
    SysRoleService roleService;

    @Override
    protected BaseRepo getThisRepo() {
        return repo;
    }


    @Override
    public void putRoles(Long userId, List<Long> roleIdList) {
        SysUserEntity entity = this.findById(userId);
        for (Long roleId :
                roleIdList) {
            SysRoleEntity roleEntity = roleService.findById(roleId);
            if (roleEntity != null) {
                entity.getRoles().add(roleEntity);
            }
        }
        this.save(entity);
    }

    @Override
    public void moveOutRoles(Long userId, List<Long> roleIdList) {
        SysUserEntity entity = this.findById(userId);
        for (Long roleId : roleIdList) {
            SysRoleEntity roleEntity = roleService.findById(roleId);
            if (roleEntity != null) {
                entity.getRoles().remove(roleEntity);
            }
        }
        this.save(entity);
    }
}
